package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Upvoke;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.UpvokeMapper;
import cn.wolfcode.trip.base.query.UpvokeQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUpvokeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UpvokeServiceImpl implements IUpvokeService {
    @Autowired
    private UpvokeMapper upvokeMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Override
    public int upvoke(Upvoke upvoke) {
        int insert = upvokeMapper.insert(upvoke);
        return insert;
    }

    @Override
    public PageInfo listUpvokeInfo(UpvokeQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Upvoke> upvokes=upvokeMapper.selectForPage(qo);
        return new PageInfo(upvokes);
    }

    @Override
    public void updateState(int isnew, Long userId) {
        upvokeMapper.updateStateByUserId(isnew,userId);
    }

    public List<Upvoke> hasStategyLikes(Upvoke upvoke) {
        return upvokeMapper.hasStategyLikes(upvoke);
    }

    /**
     * 攻略取消点赞的业务方法
     * 两个作用,一个是给upvoke做删除,一个给strategy做updata
     */
    @Override
    public void deleteByStrategyIdForStrategy(Upvoke upvoke) {
        //删除upvoke
        upvokeMapper.deleteByPrimaryKey(upvoke.getId());
        //strategy中likeNum减1
        strategyMapper.updateByDeleteLikeNum(upvoke.getRelationId());
    }

    /**
     *  游记取消点赞方法
     */
    @Override
    public void deleteByStrategyIdForTravel(Upvoke upvoke) {
        upvokeMapper.deleteByPrimaryKey(upvoke.getId());
        travelService.updateByDeleteLikeNum(upvoke.getRelationId());
    }

    /**
     * 判断游记内容是否已被点赞方法,刷新之后样式不会变
     * @return
     */
    @Override
    public Upvoke queryForUpvoke(Upvoke upvoke) {
        return upvokeMapper.selectForUpvoke(upvoke);

    }

    /**
     * 攻略点评取消点赞的业务方法
     * 两个作用,一个是给upvoke做删除,一个给strategyCommend做updata
     */
    @Override
    public void deleteByStrategyIdForStrategyComment(Upvoke upvoke) {
        //删除upvoke
        upvokeMapper.deleteByPrimaryKey(upvoke.getId());
        //strategy中likeNum减1
        strategyCommentMapper.updateShowLikeNum(upvoke.getRelationId());
    }


}
